package com.wzxy.wzinfo.controller;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wzxy.wzinfo.entiry.NewsIntroduce;
import com.wzxy.wzinfo.entiry.Operation;
import com.wzxy.wzinfo.entiry.User;
import com.wzxy.wzinfo.service.impl.NewsIntService;
import com.wzxy.wzinfo.service.impl.OperationService;
import com.wzxy.wzinfo.utils.MyPage;


/*
 * springmvc请求参数获取的几种方法(还未调查新版本是否有改变)
 * 1、直接把表单的参数写在Controller相应的方法的形参中，适用于get方式提交，不适用于post方式提交。
 * 2、通过HttpServletRequest接收，post方式和get方式都可以。
 * 3、通过一个bean来接收,post方式和get方式都可以。
 * 4、通过@PathVariable获取路径中的参数
 * 5、使用@ModelAttribute注解获取POST请求的FORM表单数据
 * 6、用注解@RequestParam绑定请求参数到方法入参，当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false)
 *
 */

@Controller
@RequestMapping("newsint")
public class NewsIntController {
	
	private int pageSize = 10;
	
	@Autowired
	private NewsIntService newsIntService;
	
	@Autowired
	private OperationService operationService;
	
	/**
	 * 提供者获取分页摘要（只能查看自己提交的摘要）
	 * @param page
	 * @param size
	 * @param session
	 * @param model
	 * @return
	 * 直接把表单的参数写在Controller相应的方法的形参中，适用于get方式提交，不适用于post方式提交。
	 */
	@GetMapping("/newsInt_page")
	public String getPage(Integer page,Integer size,HttpSession session,Model model){
		if(page==null){
			page = 1;
		}
		if(size==null){
			size = pageSize;
		}
		User loginUser = (User)session.getAttribute("login_user");
		MyPage<NewsIntroduce> myPage = new MyPage<NewsIntroduce>();
		myPage.setOnePageNum(size);
		myPage.setPageNo(page);
		
		Integer loginID = loginUser.getId();
		
		if(loginUser.getType().intValue()==3){
			//如果是管理员则置空，管理员可查看所有信息
			loginID = null;
		}
		
		newsIntService.getPage(myPage,loginID,0,1);
		
		model.addAttribute("newsInts", myPage);
		model.addAttribute("pageURL", "newsInt_page");
		return "newsintr_list";
	}
	
	/**
	 * 审核人获取分页摘要（查看所有提供者提供的未审核摘要）
	 * @param page
	 * @param size
	 * @param session
	 * @param model
	 * @return
	 */
	@GetMapping("/newsInt_aprpage")
	public String getAprPage(Integer page,Integer size,HttpSession session,Model model){
		if(page==null){
			page = 1;
		}
		if(size==null){
			size = pageSize;
		}
		MyPage<NewsIntroduce> myPage = new MyPage<NewsIntroduce>();
		myPage.setOnePageNum(size);
		myPage.setPageNo(page);
		
		newsIntService.getPage(myPage,null,0,0);
		
		model.addAttribute("newsInts", myPage);
		return "index";
	}
	
	/**
	 * 所有摘要列表
	 * @param page
	 * @param size
	 * @param session
	 * @param model
	 * @return
	 */
	@GetMapping("/newsInt_aprpagelist")
	public String getAprPageList(Integer page,Integer size,HttpSession session,Model model){
		if(page==null){
			page = 1;
		}
		if(size==null){
			size = pageSize;
		}
		MyPage<NewsIntroduce> myPage = new MyPage<NewsIntroduce>();
		myPage.setOnePageNum(size);
		myPage.setPageNo(page);
		
		newsIntService.getPage(myPage,null,0,1);
		
		model.addAttribute("newsInts", myPage);
		model.addAttribute("pageURL", "newsInt_aprpagelist");
		return "newsintr_list";
	}
	
	/**
	 * 添加摘要
	 * @param newsIntroduce
	 * @param session
	 * @param model
	 * @return
	 * 通过一个bean来接收参数,post方式和get方式都可以。
	 */
	@PostMapping("/newsInt_add")
	public String add(NewsIntroduce newsIntroduce,HttpSession session,Model model){
		
		User loginUser = (User)session.getAttribute("login_user");
		//设置提供者id
		newsIntroduce.setProvideId(loginUser.getId());
		
		newsIntService.addNewsInt(newsIntroduce);
		
		return getPage(1,pageSize,session,model);
	}
	
	/**
	 * 删除摘要（伪删除）
	 * @param newsIntroduce
	 * @param reason
	 * @param session
	 * @param model
	 * @return
	 * 直接把表单的参数写在Controller相应的方法的形参中，适用于get方式提交，不适用于post方式提交。
	 */
	@PostMapping("/newsInt_delete")
	public String delete(NewsIntroduce newsIntroduce,
			@RequestParam(value="reason", required=false)String reason,HttpSession session,Model model){
		newsIntService.deleteNewsInt(newsIntroduce.getId());
		User loginUser = (User)session.getAttribute("login_user");
		//设置删除人id
		Operation operation = new Operation();
		operation.setNewsintId(newsIntroduce.getId());
		operation.setUserId(loginUser.getId());
		operation.setReason(reason);
		operation.setType(2);
		//记录操作
		operationService.recordOperation(operation);
		
		return getPage(1,pageSize,session,model);
	}
	
	/**
	 * 更新摘要
	 * @param newsIntroduce
	 * @param reason
	 * @param session
	 * @param model
	 * @return
	 */
	@PostMapping("/newsInt_update")
	public String update(NewsIntroduce newsIntroduce,@RequestParam(value="reason", required=false)String reason,HttpSession session,Model model){
		
		User loginUser = (User)session.getAttribute("login_user");
		//设置删除人id
		Operation operation = new Operation();
		operation.setNewsintId(newsIntroduce.getId());
		operation.setUserId(loginUser.getId());
		operation.setReason(reason);
		operation.setType(1);
		
		//更改信息
		newsIntService.update(newsIntroduce, newsIntroduce.getId());
		//记录操作
		operationService.recordOperation(operation);
		
		return getPage(1,pageSize,session,model);
	}
	
	/**
	 * 打开指定的要审核的摘要
	 * @param newsIntroduce
	 * @return
	 */
	@GetMapping("/openApprEdit")
	public String openApprEdit(NewsIntroduce newsIntroduce,Model model){
		newsIntroduce =  newsIntService.findOne(newsIntroduce.getId());
		model.addAttribute("newsInt",newsIntroduce);
		return "appr_edit";
	}
	
	/**
	 * 审核信息
	 * @param newsIntroduce
	 * @param session
	 * @param model
	 * @return
	 */
	@PostMapping("/apprNews")
	public String apprNews(NewsIntroduce newsIntroduce,@RequestParam(value="page", required=false)Integer page, HttpSession session,Model model){
		User loginUser = (User)session.getAttribute("login_user");
		newsIntroduce.setApproveId(loginUser.getId());
		newsIntroduce = newsIntService.apprNews(newsIntroduce);
		return getAprPage(page,pageSize,session,model);
	}
	
	/**
	 * 审核人删除审核信息
	 * @param newsIntroduce
	 * @param reason
	 * @param page
	 * @param session
	 * @param model
	 * @return
	 * 直接把表单的参数写在Controller相应的方法的形参中，适用于get方式提交，不适用于post方式提交。
	 */
	@PostMapping("/apprDelete")
	public String apprDelete(NewsIntroduce newsIntroduce,@RequestParam(value="reason", required=false)String reason,
			@RequestParam(value="page", required=false)Integer page ,
			HttpSession session,Model model){
		newsIntService.deleteNewsInt(newsIntroduce.getId());
		User loginUser = (User)session.getAttribute("login_user");
		//设置删除人id
		Operation operation = new Operation();
		operation.setNewsintId(newsIntroduce.getId());
		operation.setUserId(loginUser.getId());
		operation.setReason(reason);
		operation.setType(2);
		//记录操作
		operationService.recordOperation(operation);
		return getAprPage(page,pageSize,session,model);
	}
	
	@GetMapping("/apprHistory")
	public String apprHistory(Integer page,Integer size,HttpSession session,Model model){
		if(page==null){
			page = 1;
		}
		if(size==null){
			size = pageSize;
		}
		MyPage<NewsIntroduce> myPage = new MyPage<NewsIntroduce>();
		myPage.setOnePageNum(size);
		myPage.setPageNo(page);
		newsIntService.getPage(myPage,null,1,1);
		model.addAttribute("newsInts", myPage);
		return "appr_history";
	}
	
	@PostMapping("/apprHistoryDelete")
	public String apprHistoryDelete(NewsIntroduce newsIntroduce,@RequestParam(value="page", required=false)Integer page ,
			HttpSession session,Model model){
		newsIntService.deleteNewsInt(newsIntroduce.getId());
		User loginUser = (User)session.getAttribute("login_user");
		//设置删除人id
		Operation operation = new Operation();
		operation.setNewsintId(newsIntroduce.getId());
		operation.setUserId(loginUser.getId());
		operation.setReason("删除历史记录");
		operation.setType(2);
		//记录操作
		operationService.recordOperation(operation);
		return apprHistory(page,pageSize,session,model);
	}
	
	@PostMapping("/getLikeApprNewsInts")
	public String getLikeNewsInts(NewsIntroduce newsIntroduce,@RequestParam(value="page", required=false)Integer page ,
			HttpSession session,Model model){
		
		MyPage<NewsIntroduce> myPage = new MyPage<NewsIntroduce>();
		myPage.setPageNo(page);
		newsIntService.getLikeNewsInts(myPage,newsIntroduce,0,0);
		
		model.addAttribute("newsInts", myPage);
		
		//设置模糊查询参数，下一页使用
		if(newsIntroduce.getTitle()!=null){
			model.addAttribute("likeParamName","title");
			model.addAttribute("likeParam",newsIntroduce.getTitle());
		}else if(newsIntroduce.getIntroduce()!=null){
			model.addAttribute("likeParamName","introduce");
			model.addAttribute("likeParam",newsIntroduce.getIntroduce());
		}else if(newsIntroduce.getURL()!=null){
			model.addAttribute("likeParamName","URL");
			model.addAttribute("likeParam",newsIntroduce.getURL());
		}
		return "index";
	}
	
	@PostMapping("/getLikeApprHistory")
	public String getLikeApprHistory(NewsIntroduce newsIntroduce,@RequestParam(value="page", required=false)Integer page ,
			HttpSession session,Model model){
		
		MyPage<NewsIntroduce> myPage = new MyPage<NewsIntroduce>();
		myPage.setPageNo(page);
		newsIntService.getLikeNewsInts(myPage,newsIntroduce,1,1);
		
		model.addAttribute("newsInts", myPage);
		
		//设置模糊查询参数，下一页使用
		if(newsIntroduce.getTitle()!=null){
			model.addAttribute("likeParamName","title");
			model.addAttribute("likeParam",newsIntroduce.getTitle());
		}else if(newsIntroduce.getIntroduce()!=null){
			model.addAttribute("likeParamName","introduce");
			model.addAttribute("likeParam",newsIntroduce.getIntroduce());
		}else if(newsIntroduce.getURL()!=null){
			model.addAttribute("likeParamName","URL");
			model.addAttribute("likeParam",newsIntroduce.getURL());
		}
		return "appr_history";
	}
	
	@PostMapping("/getLikeProNewsInt")
	public String getLikeProNewsInt(NewsIntroduce newsIntroduce,@RequestParam(value="page", required=false)Integer page ,
			HttpSession session,Model model){
		
		MyPage<NewsIntroduce> myPage = new MyPage<NewsIntroduce>();
		myPage.setPageNo(page);
		User loginUser = (User)session.getAttribute("login_user");
		//设置提供者id
		newsIntroduce.setProvideId(loginUser.getId());
		
		newsIntService.getLikeNewsInts(myPage,newsIntroduce,0,1);
		
		model.addAttribute("newsInts", myPage);
		
		//设置模糊查询参数，下一页使用
		if(newsIntroduce.getTitle()!=null){
			model.addAttribute("likeParamName","title");
			model.addAttribute("likeParam",newsIntroduce.getTitle());
		}else if(newsIntroduce.getIntroduce()!=null){
			model.addAttribute("likeParamName","introduce");
			model.addAttribute("likeParam",newsIntroduce.getIntroduce());
		}else if(newsIntroduce.getURL()!=null){
			model.addAttribute("likeParamName","URL");
			model.addAttribute("likeParam",newsIntroduce.getURL());
		}
		return "newsintr_list";
	}
}
